﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bank
{
    public partial class MainForm : Form
    {
        ATM atm;
        ATM creditAtm;
        int type;

        public MainForm(ATM atm, ATM creditAtm, double credit, int type)
        {
            this.atm = atm;
            this.creditAtm = creditAtm;
            this.type = type;
            InitializeComponent();
            if (type == 0)
            {
                labelCredit.Text = credit.ToString();
                labelMoney.Text = atm.getMoney().ToString();
                labelType.Text = "储蓄账户";
            }
            else
            {
                labelCredit.Text = credit.ToString();
                labelMoney.Text = creditAtm.getMoney().ToString();
                labelType.Text = "信用账户";
            }

            this.atm.BigMoneyFetched += Atm_BigMoneyFetched;
            this.creditAtm.BigMoneyFetched += Atm_BigMoneyFetched;
        }

        private void Atm_BigMoneyFetched(object sender, BigMoneyArgs e)
        {
            MessageBox.Show($"警告：账号 {e.Account} 取走大笔金额：{e.Amount} 元！");
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                double money = (double)numericUpDown1.Value;
                // 储蓄账户存款
                if (type == 0)
                {
                    if (atm.SaveMoney(money))
                    {
                        labelMoney.Text = atm.getMoney().ToString();
                        MessageBox.Show("存款成功！");
                    }
                    else
                    {
                        MessageBox.Show("存款失败！");
                    }
                }
                // 信用账户存款
                else
                {
                    if (creditAtm.SaveMoney(money))
                    {
                        labelMoney.Text = creditAtm.getMoney().ToString();
                        MessageBox.Show("存款成功！");
                    }
                    else
                    {
                        MessageBox.Show("存款失败！");
                    }
                }
            }
            // 处理坏钞票异常
            catch (BadCashException ex)
            {
                MessageBox.Show(ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonWithdraw_Click(object sender, EventArgs e)
        {
            try
            {
                double money = (double)numericUpDown1.Value;
                // 储蓄账户取款
                if (type == 0)
                {
                    if (atm.WithdrawMoney(money))
                    {
                        labelMoney.Text = atm.getMoney().ToString();
                        MessageBox.Show("取款成功！");
                    }
                    else
                    {
                        MessageBox.Show("余额不足，取款失败！");
                    }
                }
                // 信用账户取款
                else
                {
                    if (creditAtm.WithdrawMoney(money))
                    {
                        labelMoney.Text = creditAtm.getMoney().ToString();
                        MessageBox.Show("取款成功！");
                    }
                    else
                    {
                        MessageBox.Show("超过信用额度，取款失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            try
            {
                // 储蓄账户退出登录
                if(type == 0)
                {
                    atm.logout();
                }
                // 信用账户退出登录
                else
                {
                    creditAtm.logout();
                }
                Form1 form1 = new Form1(atm, creditAtm);
                form1.Show();
                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                // 储蓄账户删除账户
                if (type == 0)
                {
                    if (atm.CloseAccount())
                    {
                        MessageBox.Show("注销成功！");
                        Form1 form1 = new Form1(atm, creditAtm);
                        form1.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("注销失败！");
                    }
                }
                // 信用账户删除账户
                else
                {
                    if (creditAtm.CloseAccount())
                    {
                        MessageBox.Show("注销成功！");
                        Form1 form1 = new Form1(atm, creditAtm);
                        form1.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("注销失败！");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
